package model.quality;

import java.util.HashMap;

import javax.faces.context.FacesContext;

import model.base.CompanyFacade;

import control.ViewControl;

public class TestManager {

	public boolean testing=false;
	private ViewControl viewControl;
	private LogAssert assertive;
	private volatile static TestManager uniqueInstance;
	private CompanyFacade companyFacade;
	private HashMap objects=new HashMap();
	
	public CompanyFacade getCompanyFacade() {
		return companyFacade;
	}

	public void setCompanyFacade(CompanyFacade companyFacade) {
		this.companyFacade = companyFacade;
	}


	public boolean isTesting() {
		return testing;
	}

	public void setTesting(boolean testing) {
		this.testing = testing;
	}
	
	
	private TestManager(){}
	public static TestManager getInstance(){
		if (uniqueInstance==null){
			synchronized (TestManager.class){
				if (uniqueInstance==null){
					uniqueInstance=new TestManager();
				}
			}
		}
		return uniqueInstance;
	}

	public ViewControl getViewControl() {
		if (testing){
			return viewControl;
		}
		else{
			return (ViewControl)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("viewcontrol");
		}
	}

	@SuppressWarnings("unchecked")
	public void setViewControl(ViewControl vc) {
		if (testing){
			this.viewControl = vc;		
		}
		else{
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("viewcontrol",vc);
		}
	}

	public LogAssert getAssertive() {
		if (testing){
			return assertive;
		}
		else{
			return (LogAssert)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("assertive");
		}
	}

	@SuppressWarnings("unchecked")
	public void setAssertive(LogAssert assertive) {
		if (testing){
			this.assertive = assertive;		
		}
		else{
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("assertive",assertive);
		}
	}	
	
	public void addParameter(String kind, String par, Object obj){
		if (testing){
			objects.put(par,obj);		
		}
		else if (kind=="request"){
			FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().put(par,(String) obj);
		}
		else if (kind=="session"){			
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(par,obj);
		}		
	}

	public Object getParameter(String kind, String par){
		if (testing){
			return objects.get(par);		
		}
		else if (kind=="request"){
			return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(par);
		}
		else if (kind=="session"){			
			return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(par);
		}		
		return null;
	}	
	
}
